'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE, DECIMAL } = app.Sequelize;

  const Order = app.model.define('order', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    orderNo: { type: STRING(50), allowNull: false, unique: true, comment: '订单编号' },
    memberId: { type: INTEGER, allowNull: false, comment: '会员ID' },
    totalAmount: { type: DECIMAL(10, 2), allowNull: false, comment: '订单总金额' },
    status: { 
      type: INTEGER, 
      defaultValue: 1, 
      comment: '订单状态：1-待支付，2-已支付待发货，3-已发货，4-已完成，0-已取消' 
    },
    paymentMethod: { type: STRING(20), comment: '支付方式' },
    paymentTime: { type: DATE, comment: '支付时间' },
    shippingAddress: { type: STRING(255), comment: '收货地址' },
    shippingContact: { type: STRING(50), comment: '收货人' },
    shippingPhone: { type: STRING(20), comment: '收货电话' },
    shippingTime: { type: DATE, comment: '发货时间' },
    remark: { type: STRING(255), comment: '备注' },
    createdAt: DATE,
    updatedAt: DATE,
  });

  Order.associate = function() {
    app.model.Order.belongsTo(app.model.Member, { foreignKey: 'memberId', as: 'member' });
    app.model.Order.hasMany(app.model.OrderItem, { foreignKey: 'orderId', as: 'items' });
  };

  return Order;
};
